package com.shushan.mixedexam.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shushan.mixedexam.bean.ExerciseBean;
import com.shushan.mixedexam.bean.ExerciseOptionBean;
import com.shushan.mixedexam.bean.MutiExerciseBean;
import com.shushan.mixedexam.bean.MutiExerciseOptionBean;
import com.shushan.mixedexam.bean.NewTypeOptionBean;
import com.shushan.mixedexam.strangewords.bean.StrangeWordsExerciseBean;
import com.shushan.shushanbao.R;
import com.shushan.util.ImageUtils;

import java.util.List;


/**
 * @ClassName: AnswerAdapter
 * @Desciption: 生字练习答案选项列表的adapter
 * @author: zhangshihao
 * @date: 2018-07-25
 */
public class MutiExerciseAnswerAdapter extends BaseAdapter{

    private static final String TAG = "MutiExerciseAnswerAdapter";

    private Context context;
    private MutiExerciseBean exerciseBean;
    private List<MutiExerciseOptionBean> exerciseOptionList;
    private String [] aLabels;
    private boolean isOnlyDisplay = false;

    public MutiExerciseAnswerAdapter(Context context, MutiExerciseBean exerciseBean) {
        this.context = context;
        this.exerciseBean = exerciseBean;
        this.exerciseOptionList = exerciseBean.getOptionBeans();

        aLabels = context.getResources().getStringArray(R.array.exam_answer_label_array);
    }

    @Override
    public int getCount() {
        return exerciseOptionList.size();
    }

    @Override
    public Object getItem(int position) {
        return exerciseOptionList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (null == convertView) {
            //填充布局
            convertView =  LayoutInflater.from(context).inflate(R.layout.exam_answer_list_item, parent,false);
            holder = new ViewHolder();
            //通过ID查询控件
            holder.labelTextView = convertView.findViewById(R.id.exam_answer_label);
            holder.answerImageView = convertView.findViewById(R.id.exam_answer_img);
            holder.answerTextView = convertView.findViewById(R.id.exam_answer_tv);
            holder.answerTextImg = convertView.findViewById(R.id.exam_answer_text_img);
            holder.answerLayout = convertView.findViewById(R.id.ll_answer_layout);

            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }

        MutiExerciseOptionBean exerciseOptionBean = exerciseOptionList.get(position);

        holder.labelTextView.setText(aLabels[position]);

        if(exerciseOptionBean.content_type.equals("TEXT")){
            holder.answerTextView.setVisibility(View.VISIBLE);
            holder.answerTextView.setText(exerciseOptionBean.option_content);
            holder.answerTextImg.setVisibility(View.GONE);
        }else {
            holder.answerTextView.setVisibility(View.GONE);
            holder.answerTextImg.setVisibility(View.VISIBLE);
            if(holder.answerTextView.getTag() == null || (Integer)holder.answerTextView.getTag() != position) {
                ImageUtils.loadImageNoBg(context, exerciseOptionBean.option_imgs, holder.answerTextImg);
                holder.answerTextView.setTag(position);
            }
        }

        int answerIndex = exerciseOptionBean.getAnswerIndex();

        holder.answerImageView.setVisibility(View.GONE);
        if(isOnlyDisplay){

            boolean isContainOption = false;
            if(answerIndex == position){
                isContainOption = true;
            }else if(exerciseBean != null){

                isContainOption = (exerciseBean.my_answer_id == exerciseOptionBean.id);
            }

            if(isContainOption){
                holder.answerImageView.setVisibility(View.VISIBLE);
                if(exerciseOptionBean.option_result == 1){
                    holder.answerImageView.setImageResource(R.drawable.exam_answer_hook);
                }else{
                    holder.answerImageView.setImageResource(R.drawable.exam_answer_wrong);
                }
            }

        }else if(answerIndex == position){
            holder.answerImageView.setVisibility(View.VISIBLE);
            if(exerciseOptionBean.option_result == 1){
                holder.answerImageView.setImageResource(R.drawable.exam_answer_hook);
                holder.answerLayout.setBackgroundResource(R.drawable.bg_stroke_blue_radius_3);
            }else{
                holder.answerImageView.setImageResource(R.drawable.exam_answer_wrong);
                holder.answerLayout.setBackgroundResource(R.drawable.bg_stroke_red);
            }
        }

        return convertView;
    }

    public static class ViewHolder{
        TextView labelTextView;
        ImageView answerImageView;
        TextView answerTextView;
        ImageView answerTextImg;
        LinearLayout answerLayout;
    }

    public List<MutiExerciseOptionBean> getExerciseOptionList() {
        return exerciseOptionList;
    }

    public boolean isOnlyDisplay() {
        return isOnlyDisplay;
    }

    public void setOnlyDisplay(boolean onlyDisplay) {
        isOnlyDisplay = onlyDisplay;
    }
}
